//
//  WYCRegisterController.m
//  51信用卡
//
//  Created by 中企互联 on 2017/11/3.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCRegisterController.h"
#import "UIButton+CountDown.h"
#import "WYCRealnameController.h"
#import "RSAUtil.h"

@interface WYCRegisterController ()

/** 账号输入框*/
@property(nonatomic,strong)UITextField * UserTextField;

/** 密码*/
@property(nonatomic,strong)UITextField * passtexfield;


/** 再次输入密码*/
@property(nonatomic,strong)UITextField * Zpasstexfield;

/** 验证码*/
@property(nonatomic,strong)UITextField * codeTexField;


/** 邀请码*/
@property(nonatomic,strong)UITextField * Rgscodefield;

/** 密码隐藏或显示*/
@property(nonatomic,strong)UIButton * ZpasstexfieldButton;

/** 密码隐藏或显示*/
@property(nonatomic,strong)UIButton * passtexfieldButton;

@end

@implementation WYCRegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //创建界面
    [self setupCreat];
    
}
#pragma make ------->>创建界面
- (void)setupCreat{
    
    self.view.backgroundColor = wycwhiteColor;
    
    
    
    UIView * registerView = [[UIView alloc] init];
    registerView.frame = CGRectMake(0, 0, ScreenWidth, 64);
    registerView.backgroundColor = WYCMain;
    [self.view addSubview:registerView];
    
    
    //不是Nav。所以自己写的View和label
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
     NavRigsterLabel.frame = CGRectMake(ScreenWidth / 2 - 100 * px, 30, 200 * px, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    //    NavRigsterLabel.font = [UIFont systemFontOfSize:BigFont];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"注册";
    [registerView addSubview:NavRigsterLabel];
    
    
    
    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(30 * px, NavRigsterLabel.y, 66 * px , 66 * px);
    [registerView addSubview:backbutton];
    
    
    ////////////////
    //账号输入框/////
    ///////////////
    UIView *iponeView = [[UIView alloc] init];
    iponeView.frame = CGRectMake(80 * px, CGRectGetMaxY(registerView.frame) + 80 * px, ScreenWidth - 160 * px, 150 * px);
    iponeView.layer.cornerRadius = 75 * px;
    iponeView.layer.borderWidth = 0.5;
    iponeView.layer.borderColor = [UIColor grayColor].CGColor;
    [self.view addSubview:iponeView];
    
    
    //手机号
    UIImageView * iphonelabel = [[UIImageView alloc] init];
    iphonelabel.frame = CGRectMake(40 * px, 25 * px, 100 * px, 100 * px);
    iphonelabel.image = [UIImage imageNamed:@"手机-icon"];
    [iponeView addSubview:iphonelabel];
    
    
    UIView *iponeline = [[UIView alloc] init];
    iponeline.frame = CGRectMake(CGRectGetMaxX(iphonelabel.frame) + 20 * px, iphonelabel.y, 1, iphonelabel.height);
    iponeline.backgroundColor = [UIColor grayColor];
    [iponeView addSubview:iponeline];
    
    UITextField * userTextField = [[UITextField alloc] init];
    userTextField.frame = CGRectMake(CGRectGetMaxX(iponeline.frame) + 10 * px, iphonelabel.y, iponeView.width - iphonelabel.width + 10 * px, iphonelabel.height);
    //    userTextField.backgroundColor = [UIColor whiteColor];
    userTextField.placeholder = @"请输入手机号";
    userTextField.keyboardType = UIKeyboardTypeNumberPad;
    //    userTextField.text = @"13578728678";
    [iponeView addSubview:userTextField];
    self.UserTextField = userTextField;
    
    UIButton * usertexfieldButton = [[UIButton alloc] init];
    usertexfieldButton.frame = CGRectMake(userTextField.width - 40 * px, userTextField.y, 80 * px, 80 * px);
    [usertexfieldButton addTarget:self action:@selector(usertexfieldButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [usertexfieldButton setImage:[UIImage imageNamed:@"celar"] forState:UIControlStateNormal];
    [iponeView addSubview:usertexfieldButton];
    
    
    ////////////////
    /////密码输入框/////
    ///////////////
    UIView *codeView = [[UIView alloc] init];
    codeView.frame = CGRectMake(80 * px, CGRectGetMaxY(iponeView.frame) + 80 * px, ScreenWidth - 160 * px, 150 * px);
    codeView.layer.cornerRadius = 75 * px;
    codeView.layer.borderWidth = 0.5;
    codeView.layer.borderColor = [UIColor grayColor].CGColor;
    [self.view addSubview:codeView];
    
    
    //手机号
    UIImageView * codelabel = [[UIImageView alloc] init];
    codelabel.frame = CGRectMake(40 * px, 25 * px, 100 * px, 100 * px);
    codelabel.image = [UIImage imageNamed:@"yanzhengma_icon"];
    [codeView addSubview:codelabel];
    
    
    UIView *codeline = [[UIView alloc] init];
    codeline.frame = CGRectMake(CGRectGetMaxX(codelabel.frame) + 20 * px, codelabel.y, 1, iphonelabel.height);
    codeline.backgroundColor = [UIColor grayColor];
    [codeView addSubview:codeline];
    
    UITextField * codeField = [[UITextField alloc] init];
    codeField.frame = CGRectMake(CGRectGetMaxX(codeline.frame) + 10 * px, codelabel.y, iponeView.width - iphonelabel.width + 10 * px, iphonelabel.height);
    codeField.placeholder = @"请输入验证码";
    codeField.keyboardType = UIKeyboardTypeNumberPad;
    [codeView addSubview:codeField];
    _codeTexField = codeField;
    
    
    
   
    
//    //手机提示线
//    UIView * passlineView = [[UIView alloc] init];
//    passlineView.frame = CGRectMake(codefield.x, CGRectGetMaxY(codefield.frame) + 5 * px, iponeView.width, 1);
//    passlineView.backgroundColor = WYCGlobalBg;
//    [self.view addSubview:passlineView];
//
    //获取验证码
    UIButton * codebutton = [[UIButton alloc] init];
    codebutton.frame = CGRectMake(ScreenWidth - 510 * px, codeField.y, 300 * px, codeField.height);
    [codebutton setTitleColor:WYCMain forState:UIControlStateNormal];
    codebutton.titleLabel.font = [UIFont systemFontOfSize:SmallFont];
    [codebutton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [codebutton addTarget:self action:@selector(countDownBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [codeView addSubview:codebutton];
    
    
    
    ////////////////
    //密码输入框/////
    ///////////////
    
    
    UIView *passView = [[UIView alloc] init];
    passView.frame = CGRectMake(80 * px, CGRectGetMaxY(codeView.frame) + 80 * px, ScreenWidth - 160 * px, 150 * px);
    passView.layer.cornerRadius = 75 * px;
    passView.layer.borderWidth = 0.5;
    passView.layer.borderColor = [UIColor grayColor].CGColor;
    [self.view addSubview:passView];
    
    
    //手机号
    UIImageView * passlabel = [[UIImageView alloc] init];
    passlabel.frame = CGRectMake(40 * px, 25 * px, 100 * px, 100 * px);
    passlabel.image = [UIImage imageNamed:@"密码-icon"];
    [passView addSubview:passlabel];
    
    
    UIView *passline = [[UIView alloc] init];
    passline.frame = CGRectMake(CGRectGetMaxX(iphonelabel.frame) + 20 * px, iphonelabel.y, 1, iphonelabel.height);
    passline.backgroundColor = [UIColor grayColor];
    [passView addSubview:passline];
    
    UITextField * passTextField = [[UITextField alloc] init];
    passTextField.frame = CGRectMake(CGRectGetMaxX(passline.frame) + 10 * px, passlabel.y, iponeView.width - iphonelabel.width + 10 * px, iphonelabel.height);
    //    userTextField.backgroundColor = [UIColor whiteColor];
    passTextField.placeholder = @"请输入密码";
    passTextField.secureTextEntry = YES;
//    passTextField.keyboardType = UIKeyboardTypeNumberPad;
    //    userTextField.text = @"13578728678";
    [passView addSubview:passTextField];
    self.passtexfield = passTextField;
    
    
    UIButton * passtexfieldButton = [[UIButton alloc] init];
    passtexfieldButton.frame = CGRectMake(passTextField.width - 40 * px, passTextField.y, 80 * px, 80 * px);
    [passtexfieldButton addTarget:self action:@selector(passtexfieldButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [passtexfieldButton setImage:[UIImage imageNamed:@"隐藏"] forState:UIControlStateNormal];
    [passtexfieldButton setImage:[UIImage imageNamed:@"展示"] forState:UIControlStateSelected];
    [passView addSubview:passtexfieldButton];
    self.passtexfieldButton = passtexfieldButton;
    
    
    
    /************再次输入密码************/
    
    UIView *ZpassView = [[UIView alloc] init];
    ZpassView.frame = CGRectMake(80 * px, CGRectGetMaxY(passView.frame) + 80 * px, ScreenWidth - 160 * px, 150 * px);
    ZpassView.layer.cornerRadius = 75 * px;
    ZpassView.layer.borderWidth = 0.5;
    ZpassView.layer.borderColor = [UIColor grayColor].CGColor;
    [self.view addSubview:ZpassView];
    
    
    //手机号
    UIImageView * Zpasslabel = [[UIImageView alloc] init];
    Zpasslabel.frame = CGRectMake(40 * px, 25 * px, 100 * px, 100 * px);
    Zpasslabel.image = [UIImage imageNamed:@"密码-icon"];
    [ZpassView addSubview:Zpasslabel];
    
    
    UIView *Zpassline = [[UIView alloc] init];
    Zpassline.frame = CGRectMake(CGRectGetMaxX(Zpasslabel.frame) + 20 * px, Zpasslabel.y, 1, iphonelabel.height);
    Zpassline.backgroundColor = [UIColor grayColor];
    [ZpassView addSubview:Zpassline];
    
    UITextField * ZpassTextField = [[UITextField alloc] init];
    ZpassTextField.frame = CGRectMake(CGRectGetMaxX(Zpassline.frame) + 10 * px, Zpasslabel.y, iponeView.width - iphonelabel.width + 10 * px, iphonelabel.height);
    //    userTextField.backgroundColor = [UIColor whiteColor];
    ZpassTextField.placeholder = @"再次输入密码";
    ZpassTextField.secureTextEntry = YES;
    [ZpassView addSubview:ZpassTextField];
    self.Zpasstexfield = ZpassTextField;
    
    UIButton * ZpasstexfieldButton = [[UIButton alloc] init];
    ZpasstexfieldButton.frame = CGRectMake(ZpassTextField.width - 40 * px, ZpassTextField.y, 80 * px, 80 * px);
    [ZpasstexfieldButton addTarget:self action:@selector(ZpasstexfieldButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [ZpasstexfieldButton setImage:[UIImage imageNamed:@"隐藏"] forState:UIControlStateNormal];
    [ZpasstexfieldButton setImage:[UIImage imageNamed:@"展示"] forState:UIControlStateSelected];
    [ZpassView addSubview:ZpasstexfieldButton];
    self.ZpasstexfieldButton = ZpasstexfieldButton;
//
    
    
    /************再次输入密码************/
    
    UIView *RgscodeView = [[UIView alloc] init];
    RgscodeView.frame = CGRectMake(80 * px, CGRectGetMaxY(ZpassView.frame) + 80 * px, ScreenWidth - 160 * px, 150 * px);
    RgscodeView.layer.cornerRadius = 75 * px;
    RgscodeView.layer.borderWidth = 0.5;
    RgscodeView.layer.borderColor = [UIColor grayColor].CGColor;
    [self.view addSubview:RgscodeView];
    
    
    //手机号
    UIImageView * Rgscodelabel = [[UIImageView alloc] init];
    Rgscodelabel.frame = CGRectMake(40 * px, 25 * px, 100 * px, 100 * px);
    Rgscodelabel.image = [UIImage imageNamed:@"yanzhengma_icon"];
    [RgscodeView addSubview:Rgscodelabel];
    
    
    UIView *Rgscodeline = [[UIView alloc] init];
    Rgscodeline.frame = CGRectMake(CGRectGetMaxX(Rgscodelabel.frame) + 20 * px, Rgscodelabel.y, 1, iphonelabel.height);
    Rgscodeline.backgroundColor = [UIColor grayColor];
    [RgscodeView addSubview:Rgscodeline];
    
    UITextField * RgscodeTextField = [[UITextField alloc] init];
    RgscodeTextField.frame = CGRectMake(CGRectGetMaxX(Rgscodeline.frame) + 10 * px, Rgscodelabel.y, iponeView.width - iphonelabel.width + 10 * px, iphonelabel.height);
    //    userTextField.backgroundColor = [UIColor whiteColor];
    RgscodeTextField.placeholder = @"输入注册码";
    RgscodeTextField.keyboardType = UIKeyboardTypeNumberPad;
    //    userTextField.text = @"13578728678";
    [RgscodeView addSubview:RgscodeTextField];
    self.Rgscodefield = RgscodeTextField;
    
    
    
    
    
    
    //注册按钮
    UIButton * loginButton = [[UIButton alloc] init];
    loginButton.frame = CGRectMake(80 * px, CGRectGetMaxY(RgscodeView.frame) + 200 * px, ScreenWidth - 160 * px, 150 * px);
    loginButton.backgroundColor = WYCMain;
    loginButton.layer.cornerRadius = 75 * px;
    [loginButton setTitle:@"注册" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:72 * px];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(RegisterClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    
    
    
    UILabel * PromptLable = [[UILabel alloc] init];
    UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:SmallFont];
    NSString * str = @"点击《注册》表示您同意";
    PromptLable.font = fnt;
    PromptLable.x = loginButton.x + 120 * px;
    PromptLable.y = CGRectGetMaxY(loginButton.frame) + 30 * px;
    CGSize PromptLableSize = [str sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:fnt,NSFontAttributeName, nil]];
    PromptLable.text = str;
    PromptLable.textColor = WYCRGBColor(157, 157, 157);
    PromptLable.frame = CGRectMake(PromptLable.x, PromptLable.y, PromptLableSize.width, PromptLableSize.height);
    [self.view addSubview:PromptLable];
    
    
    
    //用户须知的按钮
    UIButton * userAgreement = [[UIButton alloc] init];
    [userAgreement setTitle:@"【用户注册协议】" forState:UIControlStateNormal];
    userAgreement.titleLabel.font = [UIFont systemFontOfSize:43 * px];
    userAgreement.frame = CGRectMake(CGRectGetMaxX(PromptLable.frame), PromptLable.y, PromptLable.width + 90 * px, PromptLable.height);
    userAgreement.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [userAgreement setTitleColor:WYCMain forState:UIControlStateNormal];
    [self.view addSubview:userAgreement];
}
//键盘回收
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_UserTextField resignFirstResponder];
    [_codeTexField resignFirstResponder];
    [_passtexfield resignFirstResponder];
    [_Zpasstexfield resignFirstResponder];
    [_Rgscodefield resignFirstResponder];
}
#pragma make ------>>获取验证码
- (void)countDownBtnAction:(UIButton *)button
{
    WYCLog(@"----------%@",self.UserTextField.text);
    WYCLogFunc;
    if (self.UserTextField.text.length == 0) {
        
        NSString * showStr = @"手机号不能为空";
        [self showToast:showStr];
        return;
    }else{
        [button startWithTime:59 title:@"点击重新获取" countDownTitle:@"s后重新获取" mainColor:[UIColor colorWithRed:0 / 255.0 green:0 / 255.0 blue:0 / 255.0 alpha:0] countColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
        NSString * httpsStr = [NSString stringWithFormat:@"%@mobile=%@&type=%@",Rcode,self.UserTextField.text,Ucodekey];
        
        [HttpTool POST:httpsStr params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            
            if ([[responseObject objectForKey:@"code"] intValue] == 0) {
                
                [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
//                NSDictionary * dict = responseObject[@"result"];
//
//                WYCAccount * Account = [[WYCAccount alloc] initWithDic:dict];
//
//                [WYCAccountTool saveAccount:Account];
//
//                [WYCIsAppLoginTool saveIsAppLogin:YES];
                
            }else{
                
                [self showToast:msgStr];
            }
            
        } fail:^(NSURLSessionDataTask *task, NSError *error) {
            [self showToast:Failed];
        }];
        
    }
}

#pragma make ------>>注册按钮
- (void)RegisterClick {
    WYCLogFunc;
   // 手机号判断是否为空
    if(self.UserTextField.text.length == 0)
    {
        [self showToast:@"手机号不能为空"];
        return;
    }
    //验证码判断是否为空
    if(self.codeTexField.text.length == 0)
    {
        [self showToast:@"请输入验证码"];
        return;
    }
    //手机号判断是否为空
    if(self.passtexfield.text.length == 0)
    {
        [self showToast:@"密码不能为空"];
        return;
    }
    //密码判断是否为空

    if(![self.passtexfield.text isEqualToString:self.Zpasstexfield.text])
    {
        [self showToast:@"两次输入密码不一致"];
        return;
    }
    if (self.Rgscodefield.text.length == 0) {
        [self showToast:@"注册码不能为空"];
        return;
    }
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
        NSString * passStr = [NSString stringWithFormat:@"%@",self.passtexfield.text];
        NSString *pas = [RSAUtil encryptString:passStr publicKey:RSA_P_KEY];
    //
        params[@"mobile"] = self.UserTextField.text;
        params[@"password"] = pas;
        params[@"authcode"] = self.codeTexField.text;
        params[@"regCode"] = self.Rgscodefield.text;
        [HttpTool POST:Register params:params success:^(NSURLSessionDataTask *task, id responseObject) {
            if ([[responseObject objectForKey:@"code"] intValue] == 0) {
                NSDictionary * dict = [responseObject objectForKey:@"result"];
                WYCRealnameController * authen = [[WYCRealnameController alloc] init];
                [self presentViewController:authen animated:NO completion:nil];
                WYCAccount * Account = [[WYCAccount alloc] initWithDic:dict];

                [WYCAccountTool saveAccount:Account];

                [WYCIsAppLoginTool saveIsAppLogin:YES];
            }else{

                [self showToast:msgStr];
            }

        } fail:^(NSURLSessionDataTask *task, NSError *error) {
            [self showToast:Failed];
        }];
    
}
- (void)passtexfieldButtonClick{
    self.passtexfieldButton.selected = !self.passtexfieldButton.selected;
    if (self.passtexfieldButton.selected == YES) {
        self.passtexfield.secureTextEntry = NO;
    }else{
        
        self.passtexfield.secureTextEntry = YES;
    }
    
}

- (void)ZpasstexfieldButtonClick{
    self.ZpasstexfieldButton.selected = !self.ZpasstexfieldButton.selected;
    if (self.Zpasstexfield.selected == YES) {
        self.Zpasstexfield.secureTextEntry = NO;
    }else{
        
        self.Zpasstexfield.secureTextEntry = YES;
    }
    
}
- (void)usertexfieldButtonClick{
    
    self.UserTextField.text = @"";
}



#pragma make ------>>返回按钮
- (void)backClick{
    [self dismissViewControllerAnimated:NO completion:nil];
    
}

@end
